<?php

if(!defined('__CORE_DIR')){
    exit("Access Denied");
}
/**
 * Created by PhpStorm.
 * User: ndong
 * Date: 2016-11-15
 * Time: 22:16
 */
class Mdl_Clouddesign_Design extends Mdl_Table
{

    protected $_table = 'cloud_design';
    protected $_pk = 'clouddesignid';
    protected $_cols = 'clouddesignid,uid,user_name,designid,mainPic,dateline';
    protected $_orderby = array( 'dateline'=>'DESC');

    /**
     * 组合接口请求参数
     * */
    private  function getpublicurl($appuid='', $url=''){
        list($msec, $sec) = explode(' ', microtime());
        $msectime =  (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
        $url  = "http://www.kujiale.com" . $url;
        $url .= '&appkey=I7dGIbtfhw&timestamp='. $msectime;

        if($appuid){
            $url .= '&sign=' . MD5('9IOpkzfCzYEowFGXc4LlLKiW1Haw5fiw' . 'I7dGIbtfhw' . $appuid . $msectime);
        }else{
            $url .= '&sign=' . MD5('9IOpkzfCzYEowFGXc4LlLKiW1Haw5fiw' . 'I7dGIbtfhw'  . $msectime);
        }
        return $url;
    }

    /**
     * 调用库家乐接口
     * 在控制器里面拼接url
     * */
    public  function invokeapi($appuid='', $url='', $type='POST'){
        $requesturl = $this->getpublicurl($appuid, $url);
//        if(strpos($url, '/p/openapi/design') !== false){
//            print_r($type); exit();
//        }
        $result = K::M('net/http')->http($requesturl, array(), $type);
//        if(strpos($url, '/p/openapi/design') !== false){
//            print_r(json_decode($result)); exit();
//        }
        $result = is_null(json_decode($result)) ? $result : json_decode($result) ;

        return $result;
    }

    /*-----------------------数据库访问方法---------------------------*/
    //判断是否已经推荐
    public function is_exist($designid){
        $sql = "SELECT COUNT(*) FROM ".$this->table($this->_table)." WHERE designid='$designid'";
        $rs = $this->db->GetOne($sql);
        //print_r($rs);exit();
        return $rs>0;
    }

    //提交推荐设计
    public function create($data, $checked=false)
    {
        if($clouddesignid = $this->db->insert($this->_table, $data, true)){
            $this->update_ext_count($data);
        }
        //print_r($clouddesignid); exit();
        return $clouddesignid;
    }

    //更新字段
    public function update($pk, $data, $checked=false)
    {
//        $this->_checkpk();
//        if(!$checked && !$data = $this->_check_schema($data,  $pk)){
//            return false;
//        }
//        print_r($data);exit();
        $result = $this->db->update($this->_table, $data, $this->field($this->_pk, $pk));
        return $result;
    }
}